import unittest
from base.get_driver import GetDriver
from page.page_login import PageLogin
from parameterized import parameterized
from tools.read_txt import read_txt
import time


def get_login_data():
    arrs = []
    for data in read_txt("login_data.txt"):
        arrs.append(tuple(data.strip().split(",")))
    return arrs[1:]


class TestLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        try:
            cls.driver = GetDriver().get_driver()
            cls.login = PageLogin(cls.driver)
            cls.login.page_click_login_link()
        except Exception as e:
            # 记录日志
            print(e);

    @classmethod
    def tearDownClass(cls):
        # 关闭连接
        GetDriver().quit_driver()



    @parameterized.expand(get_login_data())
    def test_login(self, username, password, code, expect_result, state):
        self.login.page_login(username, password, code)
        if state == 'true':
            self.assertTrue(self.login.page_if_login_success())
            self.login.page_click_logout_link()
            self.login.page_click_login_link()
        else:
            time.sleep(2)
            error_info = self.login.page_get_error_info()
            self.assertEqual(error_info,expect_result)
            self.login.page_click_error_alert()
